﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Unit_2_24._10._2015
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            ChangeColor(trackBar1.Value, trackBar2.Value, trackBar3.Value);
        }

        private void ChangeColor(int red, int green, int blue)
        {
            panel1.BackColor = Color.FromArgb(red, green, blue);

            string hex = Convert.ToString(red, 16).PadLeft(2, '0') + Convert.ToString(green, 16).PadLeft(2, '0') + Convert.ToString(blue, 16).PadLeft(2, '0');
            label1.Text = string.Format("R: {0} G: {1} B: {2} HTML: #{3}", red, green, blue, hex);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random r = new Random();

            int cr = r.Next(0, 256);
            int cg = r.Next(0, 256);
            int cb = r.Next(0, 256);

            ChangeColor(cr, cg, cb);

            trackBar1.Value = cr;
            trackBar2.Value = cg;
            trackBar3.Value = cb;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            ChangeColor(trackBar1.Value, trackBar2.Value, trackBar3.Value);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            for (; Opacity > 0; Opacity -= 0.01)
            {
                System.Threading.Thread.Sleep(2);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings s = Properties.Settings.Default;

            if (s.Red != (byte)trackBar1.Value || s.Green != (byte)trackBar2.Value || s.Blue != (byte)trackBar3.Value)
            {

                DialogResult res = MessageBox.Show("Wyłączyć aplikacje?", "Potwierdzenie", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res == DialogResult.No)
                {
                    e.Cancel = true;
                }

                //Properties.Settings s = Properties.Settings.Default;
                s.Red = (byte)trackBar1.Value;
                s.Green = (byte)trackBar2.Value;
                s.Blue = (byte)trackBar3.Value;
                s.Left = this.Left;
                s.Top = this.Top;
                s.Width = this.Width;
                s.Height = this.Height;
                s.Save();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            trackBar1.Value = trackBar2.Value = trackBar3.Value = 0;
            ChangeColor(0, 0, 0);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            Properties.Settings s = Properties.Settings.Default;

            trackBar1.Value = s.Red;
            trackBar2.Value = s.Green;
            trackBar3.Value = s.Blue;

            Left = s.Left;
            Top = s.Top;
            Width = s.Width;
            Height = s.Height;

            ChangeColor(Properties.Settings.Default.Red, Properties.Settings.Default.Green, Properties.Settings.Default.Blue);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string hex =    "#" +
                            trackBar1.Value.ToString("X2") +
                            trackBar2.Value.ToString("X2") +
                            trackBar3.Value.ToString("X2");

            try
            {
                Clipboard.SetText(hex);
                MessageBox.Show("Skopiowano do schowka: " + hex);
            }
            catch
            {
                MessageBox.Show("Nie umieścić zawartości w schowku.");
            }
            
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            for (; Opacity < 1; Opacity += 0.01)
            {
                System.Threading.Thread.Sleep(1);
            }
        }
    }
}
